<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('参数展示名称')" prop="paramName">
            <el-input v-model="form.paramName" :placeholder="$t('参数展示名称')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('参数标识')" prop="paramCode">
            <el-input v-model="form.paramCode" :placeholder="$t('参数标识')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('映射参数')" prop="mappingCode">
            <el-input v-model="form.mappingCode" :placeholder="$t('映射参数')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('适用模板')" prop="useTemplateId">
            <el-select v-model="form.useTemplateId" :placeholder="$t('适用模板')" multiple filterable>
              <el-option
                v-for="(item, index) in templateList"
                :key="index"
                :label="item.templateName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item :label="$t('不适用模板')" prop="nonuseTemplateId">
            <el-select v-model="form.nonuseTemplateId" :placeholder="$t('不适用模板')" multiple filterable>
              <el-option
                v-for="(item, index) in templateList"
                :key="index"
                :label="item.templateName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:word" />
    </div>
  </div>
</template>

<script>
    import { getAllWordTemplate } from '@/api/system/word/template/wordOnlineTemplate.js'
    import { saveWordOnlineParamsCustom, getWordOnlineParamsCustom } from '@/api/system/word/wordOnlineParamsCustom'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  paramName: [
                      { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  paramCode: [
                      { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  mappingCode: [
                      { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
                templateList: []
            }
        },
        created() {
          this.getWordTemplate()
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getWordOnlineParamsCustom(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 获取所有模版
            getWordTemplate() {
              getAllWordTemplate().then(response => {
                this.templateList = response.data
              })
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    paramName: undefined,
                    paramCode: undefined,
                    mappingCode: undefined,
                    useTemplateId: undefined,
                    nonuseTemplateId: undefined,
                    remark: undefined,
                    createBy: undefined,
                    updateBy: undefined,
                    createTime: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveWordOnlineParamsCustom(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
